<?php

namespace app\admin\controller;

use app\service\CategoryService;
use app\admin\validate\CategoryValidate;
use think\exception\ValidateException;

class Category extends Admin
{
    public function __construct(CategoryService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index()
    {
        if (!$this->request->isPost()) {
            $this->assign("title", "分类列表");
            return $this->fetch("index");
        }
        $params = $this->request->post();
        $where = [];
        if (!empty($params["name"])) {
            $where[] = ['name', 'like', "%{$params['name']}%"];
        }
        if (!empty($params["status"])) {
            $where[] = ["status", "=", $params["status"]];
        }
        $ret = $this->service->getList($where);
        $this->result($ret);
    }

    private function save()
    {
        $params = $this->request->Post();
        $data = [
            "name" => $params["name"],
            "id" => isset($params["id"]) ? $params["id"] : 0,
            "sort" => $params["sort"]
        ];
        $action = $this->request->action();
        try {
            validate(CategoryValidate::class)->scene($action)->check($data);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $ret = $this->service->save($data);
        if ($ret) {
            $this->success("操作成功");
        } else {
            $this->error("保存失败");
        }
    }

    public function add()
    {
        if (!$this->request->isPost()) {
            $this->assign("vo", null);
            return $this->fetch("form");
        }
        $this->save();
    }

    public function edit()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("id");
            if (!$id) {
                throw new ValidateException("缺少id");
            }
            $vo = $this->service->get($id);
            if (empty($vo)) {
                throw new ValidateException("分类不存在");
            }
            $this->assign("vo", $vo);
            return $this->fetch("form");
        }
        $this->save();
    }

    private function checkStatus($id)
    {
        $ret = $this->service->get($id);
        if (empty($ret)) {
            throw new ValidateException("不存在对应的文章");
        }
        $article = $this->service->get($id, ["id"], ["articles" => function ($query) {
            $query->where("status", \app\model\Category::CATEGORY_STATUS_ACTIVE);
        }]);
        if (!empty($article)) {
            throw new ValidateException("存在上架的文章,请先删除对应的文章");
        }

    }

    public function status()
    {
        $params = $this->request->Post();
        try {
            validate(CategoryValidate::class)->scene("status")->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        if ($params["status"] == \app\model\Category::CATEGORY_STATUS_INACTIVE) {
            $this->checkStatus($params["id"]);
        }
        $this->service->setStatus($params["id"], $params["status"]);
        $this->success("操作成功");
    }
}